/**
 * Project Name:cnsy-url-short-service
 * File Name:CnsyDefaultProgressListener.java
 * Package Name:cn.cnsy123.shorturls.business.file.progress.listener 
 * Date: 2020年4月8日-上午12:32:08
 * Copyright (c) 2020, yangpengfei@360@126.com All Rights Reserved. 
 *
 */

package cn.cnsy123.shorturls.business.file.progress.listener;

import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.ProgressListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.cnsy123.shorturls.business.file.progress.models.FileProgress;
import cn.cnsy123.shorturls.business.util.FileSizeUtil;

/**
 * ClassName: CnsyDefaultProgressListener
 * 
 * @author yangpengfei
 * @date 2020年4月8日-上午12:32:08
 * @version 1.0
 * @since JDK 1.7
 */
public class CnsyDefaultProgressListener implements ProgressListener {
    
    private static final Logger logger = LoggerFactory.getLogger(CnsyDefaultProgressListener.class);
    
    private HttpSession httpSession;
    
    public CnsyDefaultProgressListener(HttpSession httpSession) {
        super();
        FileProgress fileProcess = new FileProgress();
        this.httpSession = httpSession;
        httpSession.setAttribute("upload_ps", fileProcess);
    }

    public CnsyDefaultProgressListener() {
        super();
    }

    @Override
    public void update(long pBytesRead, long pContentLength, int pItems) {
        logger.info("第{}个文件,已读取:{},共计:{},百分比:{}",pItems,FileSizeUtil.convertCustomFileSize(pBytesRead),FileSizeUtil.convertCustomFileSize(pContentLength),FileSizeUtil.fileUploadProgress(pBytesRead, pContentLength));
        FileProgress attribute = (FileProgress)httpSession.getAttribute("upload_ps");
        attribute.setBytesRead(pBytesRead);
        attribute.setContentLength(pContentLength);
        attribute.setItem(pItems);
        httpSession.setAttribute("upload_ps", attribute);
    }

}
